from dataclasses import dataclass
from datetime import datetime
from typing import List, Optional
from decimal import Decimal

from sqlalchemy import Column, String, Float, Integer, DateTime
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class Order(Base):
    """订单基础模型 (SQLAlchemy ORM Model)"""
    __tablename__ = 'orders'
    index = Column(Integer, autoincrement=True, primary_key=True)
    order_id = Column(String, unique=True)
    symbol = Column(String)
    side = Column(String)  # Buy/Sell
    order_type = Column(String)  # Market/Limit/Stop
    quantity = Column(Float)
    filled = Column(Float)
    quantity_left = Column(Float)
    limit_price = Column(Float, nullable=True)
    stop_price = Column(Float, nullable=True)
    avg_fill_price = Column(Float)
    status = Column(String)
    open_time = Column(DateTime)
    close_time = Column(DateTime)
    duration = Column(String)
    commission = Column(Float)
    expiration = Column(DateTime, nullable=True)
    multiplier = Column(Float)

    def __repr__(self):
        return f"<Order(order_id='{self.order_id}', symbol='{self.symbol}', side='{self.side}')>"


class Trade(Base):
    """交易基础模型 (SQLAlchemy ORM Model)"""
    __tablename__ = 'trades'

    index = Column(Integer, autoincrement=True, primary_key=True)
    trade_id = Column(String, unique=True)
    symbol = Column(String)
    open_datetime = Column(DateTime)
    open_price = Column(Float)
    open_qty = Column(Float)
    close_datetime = Column(DateTime)
    close_price = Column(Float)
    close_qty = Column(Float)
    direction = Column(String)
    open_order_type = Column(String)
    close_order_type = Column(String, nullable=True)  # 平仓订单类型
    trade_type = Column(String)
    side = Column(String, nullable=True)
    point_profit_per_lot = Column(Float)
    total_points = Column(Float)
    gross_profit = Column(Float)
    net_profit = Column(Float)
    commission = Column(Float)
    holding_time = Column(String)
    nth_trade = Column(Integer)
    session = Column(String)
    max_floating_profit_points = Column(Float, nullable=True)
    max_floating_profit_dollars = Column(Float, nullable=True)
    max_floating_loss_points = Column(Float, nullable=True)
    max_floating_loss_dollars = Column(Float, nullable=True)
    entry_reason = Column(String, nullable=True)
    exit_reason = Column(String, nullable=True)
    stop_loss_price = Column(Float, nullable=True)
    stop_loss_reason = Column(String, nullable=True)
    take_profit_price = Column(Float, nullable=True)
    take_profit_reason = Column(String, nullable=True)
    rating = Column(Integer, nullable=True)  # 交易打分，1-5分
    evaluation = Column(String, nullable=True)  # 交易评价
    open_rrr = Column(Float, nullable=True)  # OpenRRR: 止盈目标位/止损目标位
    close_rrr = Column(Float, nullable=True)  # CloseRRR: 单手点数/止损目标位
    fwr = Column(Float, nullable=True)  # FWR: 最大浮盈点/止损点位
    actual_risk = Column(Float, nullable=True)  # 实际风险: 最大浮亏点/止损点位
    extend = Column(Float, nullable=True)  # 后续最大盈利: 用户输入
    prr = Column(Float, nullable=True)  # 潜在盈亏比: 后续最大盈利/止损点位

    def __repr__(self):
        return f"<Trade(trade_id='{self.trade_id}', symbol='{self.symbol}', net_profit={self.net_profit})>"

@dataclass
class Position:
    """持仓信息模型"""
    symbol: str
    position: int
    trades: List[dict]  # List of open trades 